package Test::Lono;

use strict;
use warnings;

use Data::Dumper;
use Exporter;
use Test::Base -Base;
use Test::Pod::Coverage;

our @EXPORT = qw( module_ok is_deeply_normalized_array is_same_content );

sub module_ok($) {
    my $module = shift;
    return pod_coverage_ok( $module );
}

# XXX: Don't know why I need a function prototype here.
# TODO: see if this can be done with Test::Deep
sub is_deeply_normalized_array($$$) {
    my ($got, $expected, $message) = @_;

    my $got_back = _normalize_list( $got );
    my $expected_back = _normalize_list( $expected );
    
    return is_deeply( $got_back, $expected_back, $message );
}

sub is_same_content($$$) {
    my ( $got, $expected, $message ) = @_;

    my $got_back = _normalize_item( $got );
    my $expected_back = _normalize_item( $expected );
    
    return is( $got_back, $expected_back, $message ); 
}

# XXX: Don't know why I need a function prototype here.
sub _normalize_list($) {
    my $structure = shift;

    my @derefed = @{$structure};
    my @normalized = ();
    # XXX: iirc, if you mess with an item in a foreach, it actually
    # modifies the original array, but we'll create two lousy data
    # structures until i have a chance to test that out.
    foreach my $item ( @derefed ) {
        $item = _normalize_item( $item );
        push (@normalized, $item );
    }

    @normalized = sort( @normalized );
    return \@normalized;
}

sub _normalize_item($) {
    my $item = shift;

    # Boil all whitespace down to single space
    $item =~ s/[\s\n\r]+/ /g;

    # Remove leading and trailling spaces
    $item =~ s/^\s*//g;
    $item =~ s/\s*$//g;

    # Lowercase
    $item = lc( $item );

    return $item;
}

sub _pm_to_file {
    my $module = shift;
    my $file = $module;
    $file =~ s{::}{/}g;
    $file .= '.pm';
    $file = 'lib/' . $file;
    return $file;
}
    
1;
